package com.tencreat.mq.message;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.IPage;
import com.tencreat.common.config.OpenIMConfig;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.bean.ActionMessage;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.im.ImCustomer;
import com.tencreat.domain.im.dto.OpenIMCreateGroupDTO;
import com.tencreat.domain.im.dto.OpenIMGroupInfo;
import com.tencreat.domain.im.dto.OpenIMJoinGroupDTO;
import com.tencreat.domain.im.dto.OpenIMRegisterDTO;
import com.tencreat.domain.im.vo.OpenIMResultVo;
import com.tencreat.service.account.IAccountLoginCheckService;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.im.OpenIMApi;
import com.tencreat.service.mall.IMallGoodsCutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 创建客服事件 - 消息处理类
 */
@Service
@Slf4j
public class ActionMessageCustomerCreate implements IActionMessage {

    @Autowired
    IMallUserService mallUserService;
    @Autowired
    IAccountLoginCheckService loginCheckService;
    @Autowired
    OpenIMApi openIMApi;

    @Override
    public void dealMessage(ActionMessage message) {
        OpenIMConfig openIMConfig = OpenIMConfig.getOpenIMConfig();
        ImCustomer imCustomer = (ImCustomer) message.getData();

        OpenIMRegisterDTO openIMRegisterDTO = new OpenIMRegisterDTO(openIMConfig.getSecret(), imCustomer.getImId(), imCustomer.getUsername(), openIMConfig.getAvatarCustomer());
        // 添加openIM账户
        OpenIMResultVo<String> result = openIMApi.userRegister(openIMRegisterDTO);
        if(result.getErrCode() == 0){
            // 查找所有用户
            Integer pageSize = 1000;
            Integer pageNumber = 0;
            int count = mallUserService.count();
            for (int i = 0; i < count; pageNumber ++) {
                Page<MallUser> page = mallUserService.page(new Page<>(pageNumber, pageSize));
                page.getRecords().forEach(malluser ->{
                    // 加入这个用户的群
                    OpenIMJoinGroupDTO openIMJoinGroupDTO = new OpenIMJoinGroupDTO(imCustomer.getImId(), malluser.getUuid().toString());
                    OpenIMResultVo<String> resultJoinGroup = openIMApi.joinGroup(openIMJoinGroupDTO);
                });
                i += page.getSize();
            }
        }
    }
}
